package JUC_Advanced.chapter1_CompletableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: GuSongLiao
 * @Date: 2024/3/6 16:01
 * @Description: CompletableFuture 创建demo，讲解几种常用方法
 */
public class CompletableFutureBuildDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        //===============================无返回值====================================================
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println(Thread.currentThread().getName());
//        });
//
//        System.out.println(voidCompletableFuture.get());
//
//        //无返回值
//        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
//            System.out.println(Thread.currentThread().getName());
//        }, threadPool);
//
//        System.out.println(voidCompletableFuture1.get());

        //===============================有返回值====================================================
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            return "hello";
        });

        System.out.println(stringCompletableFuture.get());
    }
}
